home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / Gnucleus.cpp < prev    next >
C/C++ Source or Header  |  2000-07-15  |  13KB  |  477 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. // Gnucleus.cpp : Defines the class behaviors for the application.
  30. //
  31.  
  32. #include "stdafx.h"
  33. #include "Gnucleus.h"
  34.  
  35. #include "MainFrm.h"
  36. #include "ChildFrm.h"
  37. #include "GnucleusDoc.h"
  38. #include "PrefDialog.h"
  39. #include "ViewConnect.h"
  40. #include "ViewNode.h"
  41. #include "ViewSearch.h"
  42. #include "ViewSearchSpy.h"
  43. #include "ViewTransfer.h"
  44. #include "ViewDebug.h"
  45. #include "Hyperlink.h"
  46. #include "AnimEffect.h"
  47.  
  48. #include "GnuHash.h"
  49. #include "GnuControl.h"
  50.  
  51. #include <stdlib.h>
  52. #include <time.h>
  53.  
  54. #ifdef _DEBUG
  55. #define new DEBUG_NEW
  56. #undef THIS_FILE
  57. static char THIS_FILE[] = __FILE__;
  58. #endif
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CGnucleusApp
  62.  
  63. BEGIN_MESSAGE_MAP(CGnucleusApp, CWinApp)
  64.     //{{AFX_MSG_MAP(CGnucleusApp)
  65.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  66.     ON_COMMAND(ID_CONNECT, OnConnect)
  67.     ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
  68.     ON_COMMAND(ID_DEBUG, OnDebug)
  69.     ON_COMMAND(ID_DLG_PREFS, OnDlgPrefs)
  70.     ON_COMMAND(ID_TRANSFER, OnTransfer)
  71.     ON_COMMAND(ID_VIEW_SEARCHSPY, OnViewSearchspy)
  72.     ON_UPDATE_COMMAND_UI(ID_VIEW_SEARCHSPY, OnUpdateViewSearchSpy)
  73.     //}}AFX_MSG_MAP
  74.     // Standard file based document commands
  75. END_MESSAGE_MAP()
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CGnucleusApp construction
  79.  
  80. CGnucleusApp::CGnucleusApp()
  81. {
  82.     MasterDoc = NULL;
  83.  
  84.     m_pConnectViewTemplate = NULL;
  85.     m_pNodeViewTemplate = NULL;
  86.     m_pSearchViewTemplate = NULL;
  87.     m_pSearchSpyViewTemplate = NULL;
  88.     m_pDebugViewTemplate = NULL;
  89. }
  90.  
  91.  
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // The one and only CGnucleusApp object
  95.  
  96. CGnucleusApp theApp;
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CGnucleusApp initialization
  100.  
  101. BOOL CGnucleusApp::InitInstance()
  102. {
  103.     // Seed random generator
  104.     srand((unsigned)time(NULL));
  105.  
  106.     // Get saved hosts from cache
  107. //    ((CGnucleusDoc *) MasterDoc)->ReadHostsFile();
  108.  
  109.  
  110.  
  111.     if (!AfxSocketInit())
  112.     {
  113.         AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
  114.         return FALSE;
  115.     }
  116.  
  117.     MasterDoc = new CGnucleusDoc;
  118.  
  119.     // Standard initialization
  120.     // If you are not using these features and wish to reduce the size
  121.     //  of your final executable, you should remove from the following
  122.     //  the specific initialization routines you do not need.
  123.  
  124. #ifdef _AFXDLL
  125.     Enable3dControls();            // Call this when using MFC in a shared DLL
  126. #else
  127.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  128. #endif
  129.  
  130.     // Change the registry key under which our settings are stored.
  131.     // TODO: You should modify this string to be something appropriate
  132.     // such as the name of your company or organization.
  133.     SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  134.  
  135.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  136.  
  137.     // Load language DLL if set:
  138.     CPrefLanguage::StaticSwitchLanguge(((CGnucleusDoc*)MasterDoc));
  139.  
  140.     // Register the application's document templates.  Document templates
  141.     //  serve as the connection between documents, frame windows and views.
  142.  
  143.     m_pConnectViewTemplate = new CMultiDocTemplate(
  144.         IDR_CONNECT,
  145.         RUNTIME_CLASS(CGnucleusDoc),
  146.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  147.         RUNTIME_CLASS(CViewConnect));
  148.     AddDocTemplate(m_pConnectViewTemplate);
  149.  
  150.  
  151.     m_pNodeViewTemplate = new CMultiDocTemplate(
  152.         IDR_NODE,
  153.         RUNTIME_CLASS(CGnucleusDoc),        // document class
  154.         RUNTIME_CLASS(CMDIChildWnd),        // frame class
  155.         RUNTIME_CLASS(CViewNode));            // view class
  156.     AddDocTemplate(m_pNodeViewTemplate);
  157.  
  158.  
  159.     m_pSearchViewTemplate = new CMultiDocTemplate(
  160.         IDR_SEARCH,
  161.         RUNTIME_CLASS(CGnucleusDoc),        // document class
  162.         RUNTIME_CLASS(CMDIChildWnd),        // frame class
  163.         RUNTIME_CLASS(CViewSearch));        // view class
  164.     AddDocTemplate(m_pSearchViewTemplate);
  165.  
  166.     m_pSearchSpyViewTemplate = new CMultiDocTemplate(
  167.         IDR_VIEW_SEARCH_SPY,
  168.         RUNTIME_CLASS(CGnucleusDoc),        // document class
  169.         RUNTIME_CLASS(CMDIChildWnd),        // frame class
  170.         RUNTIME_CLASS(CViewSearchSpy));        // view class
  171.     AddDocTemplate(m_pSearchSpyViewTemplate);
  172.  
  173.     m_pDebugViewTemplate = new CMultiDocTemplate(
  174.         IDR_DEBUG,
  175.         RUNTIME_CLASS(CGnucleusDoc),        // document class
  176.         RUNTIME_CLASS(CMDIChildWnd),        // frame class
  177.         RUNTIME_CLASS(CViewDebug));        // view class
  178.     AddDocTemplate(m_pDebugViewTemplate);
  179.  
  180.  
  181.     m_pTransfersViewTemplate = new CMultiDocTemplate(
  182.             IDR_TRANSFER,
  183.             RUNTIME_CLASS(CGnucleusDoc),        // document class
  184.             RUNTIME_CLASS(CChildFrame),        // frame class
  185.             RUNTIME_CLASS(CViewTransfer));        // view class
  186.     AddDocTemplate(m_pTransfersViewTemplate);
  187.  
  188.  
  189.     // create main MDI Frame window
  190.     CMainFrame* pMainFrame = new CMainFrame;
  191.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  192.         return FALSE;
  193.     m_pMainWnd = pMainFrame;
  194.  
  195.  
  196.     // The main window has been initialized, so show and update it.
  197.     pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
  198.     pMainFrame->UpdateWindow();
  199.  
  200.     // Make Transfer window, initialize hidden
  201.     TransferFrame = m_pTransfersViewTemplate->CreateNewFrame(MasterDoc, NULL);
  202.     m_pTransfersViewTemplate->InitialUpdateFrame(TransferFrame, MasterDoc);
  203.     
  204.     TransferFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
  205.     TransferFrame->SetWindowText("Transfers");
  206.     TransferFrame->ShowWindow(SW_HIDE);
  207.  
  208.     // Show Connect Frame
  209.     ConnectFrame = m_pConnectViewTemplate->CreateNewFrame(MasterDoc, NULL);
  210.     m_pConnectViewTemplate->InitialUpdateFrame(ConnectFrame, MasterDoc);
  211.  
  212.     // Cascade windows
  213.     SendMessage(m_pMainWnd->m_hWnd, WM_COMMAND, ID_WINDOW_CASCADE, NULL);
  214.  
  215.     // Start listening for incoming connects
  216.     ((CGnucleusDoc *) MasterDoc)->GnuComm->StartListening();
  217.     
  218.     return TRUE;
  219. }
  220.  
  221.  
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CAboutDlg dialog used for App About
  224.  
  225. class CAboutDlg : public CDialog
  226. {
  227. public:
  228.     CAboutDlg();
  229.  
  230. // Dialog Data
  231.     //{{AFX_DATA(CAboutDlg)
  232.     enum { IDD = IDD_ABOUTBOX };
  233.     CHyperLink    m_HyperlinkContact2;
  234.     CHyperLink    m_HyperlinkContact1;
  235.     //}}AFX_DATA
  236.  
  237.     // ClassWizard generated virtual function overrides
  238.     //{{AFX_VIRTUAL(CAboutDlg)
  239.     protected:
  240.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  241.     //}}AFX_VIRTUAL
  242.  
  243. // Implementation
  244. protected:
  245.     //{{AFX_MSG(CAboutDlg)
  246.     afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
  247.     afx_msg void OnDestroy();
  248.     //}}AFX_MSG
  249.     DECLARE_MESSAGE_MAP()
  250. };
  251.  
  252. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  253. {
  254.     //{{AFX_DATA_INIT(CAboutDlg)
  255.     //}}AFX_DATA_INIT
  256. }
  257.  
  258. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  259. {
  260.     CDialog::DoDataExchange(pDX);
  261.     //{{AFX_DATA_MAP(CAboutDlg)
  262.     DDX_Control(pDX, IDC_STATIC_CONTACT2, m_HyperlinkContact2);
  263.     DDX_Control(pDX, IDC_STATIC_CONTACT1, m_HyperlinkContact1);
  264.     //}}AFX_DATA_MAP
  265. }
  266.  
  267. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  268.     //{{AFX_MSG_MAP(CAboutDlg)
  269.     ON_WM_SHOWWINDOW()
  270.     ON_WM_DESTROY()
  271.     //}}AFX_MSG_MAP
  272. END_MESSAGE_MAP()
  273.  
  274. void CAboutDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
  275. {
  276.     if( bShow )
  277.     {
  278.         ShowAnimEffect(this, TRUE);
  279.     }
  280.  
  281.     CDialog::OnShowWindow(bShow, nStatus);
  282. }
  283.  
  284. void CAboutDlg::OnDestroy() 
  285. {
  286.     CDialog::OnDestroy();
  287.     
  288.     ShowAnimEffect(this, FALSE);
  289. }
  290.  
  291. // App command to run the dialog
  292. void CGnucleusApp::OnAppAbout()
  293. {
  294.     CAboutDlg aboutDlg;
  295.     aboutDlg.DoModal();
  296. }
  297.  
  298. /////////////////////////////////////////////////////////////////////////////
  299. // CGnucleusApp message handlers
  300.  
  301. void CGnucleusApp::OnConnect() 
  302. {
  303.     bool Found = 0;
  304.     POSITION pos = MasterDoc->GetFirstViewPosition();
  305.  
  306.     while (pos != NULL)
  307.     {
  308.         CView* pView = MasterDoc->GetNextView(pos);
  309.         
  310.         if(pView->GetParentFrame() == ConnectFrame)
  311.         {
  312.             ConnectFrame->ShowWindow(SW_SHOWNORMAL);
  313.             ConnectFrame->BringWindowToTop();
  314.             
  315.             return;
  316.         }
  317.     }
  318.     
  319.     ConnectFrame = m_pConnectViewTemplate->CreateNewFrame(MasterDoc, NULL);
  320.     
  321.     m_pConnectViewTemplate->InitialUpdateFrame(ConnectFrame, MasterDoc);
  322. }
  323.  
  324. void CGnucleusApp::OnButtonSearch() 
  325. {
  326.     CString TestCase, Keyword, Speed;
  327.  
  328.     CComboBox *cbKeyword = (CComboBox *) ( (CMainFrame *) m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
  329.     cbKeyword->GetWindowText(Keyword);
  330.  
  331.     CString Search = "Searching for \"";
  332.             Search += Keyword;
  333.             Search += "\"";
  334.  
  335.     // Make sure search isnt null
  336.     if(Keyword == "")
  337.         return;
  338.  
  339.     // Make sure user isnt flooding searches
  340.     if(cbKeyword->GetCount() != 0)
  341.         cbKeyword->GetLBText(0, TestCase);    
  342.     
  343.     if(TestCase != Keyword)
  344.         cbKeyword->InsertString(0, Keyword);
  345.  
  346.     // Bring window to front if it already exists
  347.     bool Found = 0;
  348.     CString Title;
  349.     POSITION pos = MasterDoc->GetFirstViewPosition();
  350.  
  351.     while (pos != NULL)
  352.     {
  353.         CView* pView = MasterDoc->GetNextView(pos);
  354.         pView->GetParentFrame()->GetWindowText(Title);
  355.  
  356.         if(Title == Search)
  357.         {
  358.             pView->GetParentFrame()->BringWindowToTop();
  359.             
  360.             return;
  361.         }
  362.     }
  363.     
  364.     // Create new search window
  365.     CFrameWnd* pNewFrame = m_pSearchViewTemplate->CreateNewFrame(MasterDoc, NULL);
  366.  
  367.     pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
  368.     pNewFrame->SetWindowText(Search);
  369.  
  370.     m_pSearchViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
  371. }
  372.  
  373.  
  374. void CGnucleusApp::OnDebug() 
  375. {
  376.     //CFrameWnd* pNewFrame = m_pDebugViewTemplate->CreateNewFrame(MasterDoc, NULL);
  377.     //m_pDebugViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
  378. }
  379.  
  380. void CGnucleusApp::OnDlgPrefs() 
  381. {
  382.     CPrefDialog PrefDlg( _T("Preferences"));
  383.     PrefDlg.DoModal();    
  384. }
  385.  
  386.  
  387. void CGnucleusApp::OnTransfer() 
  388. {
  389.     TransferFrame->ShowWindow(SW_SHOW);
  390.     TransferFrame->BringWindowToTop();    
  391. }
  392.  
  393.  
  394. ////////////////////////////////////////////////////////////
  395. //! author="Nathan Brown"
  396. //
  397. //: Display the animation effect for open / close dialog
  398. void ShowAnimEffect( CWnd* pWnd, BOOL bOpen )
  399. {
  400.     CRect oRect;
  401.     AnimEffect oAnimEffect;
  402.  
  403.     if( pWnd )
  404.     {
  405.         pWnd->GetWindowRect( oRect );
  406.         bOpen ? oAnimEffect.Open(oRect) : oAnimEffect.Close(oRect);
  407.     }
  408. }
  409.  
  410.  
  411.  
  412. int CGnucleusApp::ExitInstance() 
  413. {
  414.     // In case you load multiple DLL's make sure to free them,
  415.     // and avoid calling FreeLibrary with a NULL pointer.
  416.  
  417.     if(((CGnucleusDoc*)MasterDoc)->m_hLangDLL)
  418.         FreeLibrary(((CGnucleusDoc*)MasterDoc)->m_hLangDLL);
  419.     
  420.     return CWinApp::ExitInstance();
  421. }
  422.  
  423. //
  424. //  If there is a search spy window then close it,
  425. // otherwise create one.
  426. //
  427. void CGnucleusApp::OnViewSearchspy() 
  428. {
  429.     POSITION pos = MasterDoc->GetFirstViewPosition();
  430.     CView* pView = 0;
  431.     CString Title;
  432.     
  433.     while (pos != NULL)
  434.     {
  435.         pView = MasterDoc->GetNextView(pos);
  436.         pView->GetParentFrame()->GetWindowText(Title);
  437.  
  438.         if(Title == "Search Spy" )
  439.         {
  440.             //pView->GetParentFrame()->CloseWindow();
  441.             pView->GetParentFrame()->DestroyWindow();
  442.             return;
  443.         }
  444.     }
  445.  
  446.     // Not found .. so create one.
  447.     CFrameWnd* pNewFrame = m_pSearchSpyViewTemplate->CreateNewFrame(MasterDoc, NULL);
  448.     pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
  449.     pNewFrame->SetWindowText("Search Spy");
  450.     m_pSearchSpyViewTemplate->InitialUpdateFrame(pNewFrame, MasterDoc);
  451. }
  452.  
  453. //
  454. // See if the search spy window is visible, and
  455. // set the check on the menu appropriately.
  456. //
  457. void CGnucleusApp::OnUpdateViewSearchSpy(CCmdUI *pui)
  458. {
  459.     bool Found = 0;
  460.     CString Title;
  461.     POSITION pos = MasterDoc->GetFirstViewPosition();
  462.  
  463.     while (pos != NULL)
  464.     {
  465.         CView* pView = MasterDoc->GetNextView(pos);
  466.         pView->GetParentFrame()->GetWindowText(Title);
  467.  
  468.         if(Title == "Search Spy" )
  469.         {
  470.             Found = true;            
  471.             break;
  472.         }
  473.     }
  474.  
  475.     pui->SetCheck( Found );
  476. }
  477.